<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客登陆页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
    <style>
        #verificationCodeImg {
            vertical-align: middle;
            height: 40px;
            width: 120px;
            margin-left: 30px;
        }
    </style>


</head>

<body>
    <div class="nav">
        <img src="pic/logo2.jpg" alt="">
        <span class="blog-title">逻辑博客系统</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
    </div>

    <div class="container-login">
        <div class="login-dialog">
            <h3>登陆</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" name="username" id="username">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" name="password" id="password">
            </div>
            <!-- 验证码 -->
            <div class="row">
                <span>验证码</span>
                <input type="text" name="inputCaptcha" id="inputCaptcha">
            </div>
            <div class="row">
                <img id="verificationCodeImg" src="/captcha/getCaptcha" style="cursor: pointer;" title="看不清？换一张" />
                <span>看不清?换一张</span>
            </div>
            <div class="row">
                <button id="submit" onclick="login()">提交</button>
            </div>
            <div class="row">
                <button id="submit2" onclick="registry()">注册</button>
            </div>
        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script>
        initial();
        function initial() {
            var inpArr = document.getElementsByTagName("input");
            inpArr[0].value = localStorage.getItem("username");
            inpArr[1].value = localStorage.getItem("password");
            localStorage.removeItem("username");
            localStorage.removeItem("password");
        }

        function registry() {
            var username = $("#username").val();
            if (username != null) {
                localStorage.setItem("username", username);
            }
            var password = $("#password").val();
            if (password != null) {
                localStorage.setItem("password", password);
            }
            location.href = "blog_registry.html";
        }

        function save() {
            var username = $("#username").val();
            if (username != null) {
                localStorage.setItem("username", username);
            }
            var password = $("#password").val();
            if (password != null) {
                localStorage.setItem("password", password);
            }
        }

        function login() {
            $.ajax({
                type: "post",
                url: "/captcha/check",
                data: {
                    captcha: $("#inputCaptcha").val()
                },
                //改返回格式
                success: function (results) {
                    var code = results.code;
                    if (code == 1002) {
                        save();
                        alert("请输入验证码!");
                        location.href = "blog_login.html";
                        return;
                    } else if (code == 1005) {
                        save();
                        alert("验证码错误!");
                        location.href = "blog_login.html";
                        return;
                    } else if (code == 1106) {
                        save();
                        alert("超时,验证码失效!");
                        location.href = "blog_login.html";
                        return;
                    }
                    $.ajax({
                        type: "post",
                        url: "/user/login",
                        data: {
                            "username": $("#username").val(),
                            "password": $("#password").val()
                        },
                        success: function (result) {
                            if (result.code == 200 && result.data != null) {
                                localStorage.setItem("user_token", result.data);
                                location.assign("blog_list.html");
                            } else {
                                save();
                                alert("用户名或密码错误");
                                location.href = "blog_login.html";
                            }
                        }
                    });
                }
            });

        }


        // 验证码
        $("#verificationCodeImg").click(function () {
            $(this).hide().attr('src', '/captcha/getCaptcha?dt=' + new Date().getTime()).fadeIn();
        });

        // $("#checkCaptcha").click(function () {
        // $.ajax({
        //     type:"post",
        //     url:"/captcha/check",
        //     data:{
        //     captcha: $("#inputCaptcha").val()
        //     },
        //     success:function(result) {
        //     if(result) {
        //         location.href = "success.html";
        //     }else {
        //         alert("验证失败")
        //     }
        //     }
        // });
        // });
    </script>
</body>

</html>